//
// This file is part of the aMule Project.
//
// Copyright (c) 2004-2006 Angel Vidal (Kry) ( kry@amule.org )
// Copyright (c) 2004-2006 aMule Team ( admin@amule.org / http://www.amule.org )
// Copyright (c) 2003 Barry Dunne (http://www.emule-project.net)
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
//

// Note To Mods //
/*
Please do not change anything here and release it..
There is going to be a new forum created just for the Kademlia side of the client..
If you feel there is an error or a way to improve something, please
post it in the forum first and let us look at it.. If it is a real improvement,
it will be added to the offical client.. Changing something without knowing
what all it does can cause great harm to the network if released in mass form..
Any mod that changes anything within the Kademlia side will not be allowed to advertise
there client on the eMule forum..
*/

#include "DataIO.h"
#include "Kademlia.h"
//#include "../kademlia/Tag.h"
//#include "LittleEndian.h"
#include "UInt128.h"
//#include "IOException.h"
#include "SafeFile.h"
//#include "ArchSpecific.h"
//#include <common/StringFunctions.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


////////////////////////////////////////
using namespace std;
////////////////////////////////////////

// This may look confusing that the normal methods use le() and the LE methods don't.
// The reason is the variables are stored in memory in little endian format already.

byte CDataIO::readByte()
{
	byte retVal;
	readArray(&retVal, 1);
	return retVal;
}

uint8 CDataIO::readUInt8()
{
	uint8 retVal;
	readArray(&retVal, sizeof(uint8));
	return retVal;
}

uint16 CDataIO::readUInt16()
{
	uint16 retVal;
	readArray(&retVal, sizeof(uint16));
	//return ENDIAN_SWAP_16(retVal);
	return retVal;
}

uint32 CDataIO::readUInt32()
{
	uint32 retVal;
	readArray(&retVal, sizeof(uint32));
	//return ENDIAN_SWAP_32(retVal);
	return retVal;
}

void CDataIO::readUInt128(CUInt128* value)
{
	uint32* data = (uint32*) value->getDataPtr();
	for (int i = 0; i < 4; i++)
		data[i] = readUInt32();
}

float CDataIO::readFloat()
{
	float retVal;
	readArray(&retVal, sizeof(float));
	return retVal;
}

void CDataIO::readHash(unsigned char* value)
{
	readArray(value, 16);
}

unsigned char* CDataIO::readBsob(uint8* puSize)
{
	*puSize = readUInt8();
	//if (getAvailable() < *puSize)
		//throw CIOException(ERR_BUFFER_TOO_SMALL);
	unsigned char* pucBsob = new unsigned char[*puSize];
	try {
		readArray(pucBsob, *puSize);
	} catch(...) {
		delete[] pucBsob;
		throw;
	}
	return pucBsob;
}

void CDataIO::writeByte(byte val)
{
	writeArray(&val, 1);
}

void CDataIO::writeUInt8(uint8 val)
{
	writeArray(&val, sizeof(uint8));
}

void CDataIO::writeUInt16(uint16 val)
{
	// ENDIAN_SWAP_I_16(val);
	writeArray(&val, sizeof(uint16));
}

void CDataIO::writeUInt32(uint32 val)
{
	// ENDIAN_SWAP_I_32(val);
	writeArray(&val, sizeof(uint32));
}

void CDataIO::writeUInt128(const CUInt128& val)
{
	for (int i = 0; i < 4; i++)
		writeUInt32(val.get32BitChunk(i));
}

void CDataIO::writeFloat(float val)
{
	writeArray(&val, sizeof(float));
}

void CDataIO::writeHash(const byte* value)
{
	writeArray(value, 16);
}

void CDataIO::writeBsob(const byte* value, uint8 size)
{
	writeUInt8(size);
	writeArray(value, size);
}


void CDataIO::WriteStringCore(const char *s) {
	unsigned int sLength = s ? strlen(s) : 0;
	writeUInt16(sLength);
	if (sLength) {
		// We dont include the NULL terminator.
		// It is because we write the size, so the NULL is not necessary.
		writeArray(s, sLength);
	}	
}












